package com.jiang.myfm.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiang.myfm.R;
import com.jiang.myfm.adapters.MyListenerAdapter;
import com.jiang.myfm.interfaces.SetFirstListen;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Listenfragment extends Fragment {
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private View mView;
    private List<Fragment> fragmentList;
    private SetFirstListen mSetFirstListen;

    public SetFirstListen getSetFirstListen() {
        return mSetFirstListen;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.layout_listen,container,false);
        initView();
        initTabLayout();
        initViewPager();
        return mView;
    }

    private void initViewPager() {
        fragmentList=new ArrayList<>();
        ListenFragment_1 listenFragment_1=new ListenFragment_1();
        ListenFragment_2 listenFragment_2=new ListenFragment_2();
        ListenFragment_3 listenFragment_3=new ListenFragment_3();
        fragmentList.add(listenFragment_1);
        fragmentList.add(listenFragment_2);
        fragmentList.add(listenFragment_3);
        MyListenerAdapter adapter=new MyListenerAdapter(getFragmentManager(),fragmentList);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);

        mTabLayout.getTabAt(0).setText("推荐");
        mTabLayout.getTabAt(1).setText("订阅");
        mTabLayout.getTabAt(2).setText("历史");
        mSetFirstListen = new SetFirstListen() {
            @Override
            public void onSetFirstListen(int position) {

                mTabLayout.getTabAt(position).select();
            }

        };
        mTabLayout.getTabAt(1).select();
    }

    private void initView() {
        mTabLayout= (TabLayout) mView.findViewById(R.id.tabLayout);
        mViewPager= (ViewPager) mView.findViewById(R.id.viewPager);
    }

    private void initTabLayout() {
        String[] titles=getResources().getStringArray(R.array.arrTabTitles);
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab=mTabLayout.newTab();
            tab.setText(titles[i]);
            mTabLayout.addTab(tab);
        }
        mTabLayout.setSelectedTabIndicatorColor(Color.RED);
        mTabLayout.setTabTextColors(Color.GRAY,Color.RED);
    }
}
